﻿using FluentValidation;
using Mes.Application.Common;
using Mes.Application.DTO.Inputs.Quality;
using Mes.Application.DTO.Results.Quality;
using Mes.Application.Services.Quality;
using Mes.Domain.BoundedContext;
using Microsoft.AspNetCore.Mvc;

namespace Mes.Application.Web.Controllers.Quality
{
    [ApiController]
    [Route("/api/v1")]
    public class QmSpecController : ControllerBase
    {
        private readonly IQmSpecService _qmSpecService;
        private readonly IValidator<QmSpecAddingInput> _qmSpecAddingValidator;
        private readonly IValidator<QmSpecUpdatingInput> _qmSpecUpdatingValidator;
        private readonly IValidator<QmSpecPagedInput> _qmSpecPagedValidator;
        private readonly ILogger<QmSpecController> _logger;

        public QmSpecController(IQmSpecService qmSpecService, IValidator<QmSpecAddingInput> qmSpecAddingValidator, IValidator<QmSpecUpdatingInput> qmSpecUpdatingValidator,
            IValidator<QmSpecPagedInput> qmSpecPagedValidator, ILogger<QmSpecController> logger)
        {
            _qmSpecService = qmSpecService;
            _qmSpecAddingValidator = qmSpecAddingValidator;
            _qmSpecUpdatingValidator = qmSpecUpdatingValidator;
            _qmSpecPagedValidator = qmSpecPagedValidator;
            _logger = logger;
        }

        /// <summary>
        /// 新增质量规格
        /// </summary>
        /// <param name="qmSpecInput"></param>
        /// <returns></returns>
        [HttpPost("qmspec")]
        public async Task<ActionResult> AddQmSpecAsync([FromBody] QmSpecAddingInput qmSpecInput)
        {
            try
            {
                var input = await _qmSpecAddingValidator.ValidateAsync(qmSpecInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _qmSpecService.AddQmSpecAsync(qmSpecInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 编辑质量规格
        /// </summary>
        /// <param name="qmSpecInput"></param>
        /// <returns></returns>
        [HttpPut("qmspec")]
        public async Task<ActionResult> UpdateQmSpecAsync([FromBody] QmSpecUpdatingInput qmSpecInput)
        {
            try
            {
                var input = await _qmSpecUpdatingValidator.ValidateAsync(qmSpecInput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                await _qmSpecService.UpdateQmSpecAsync(qmSpecInput);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 删除质量规格
        /// </summary>
        /// <param name="qmSpecIds"></param>
        /// <returns></returns>
        [HttpDelete("qmspec")]
        public async Task<ActionResult> DeleteQmSpecAsync(int[] qmSpecIds)
        {
            try
            {
                await _qmSpecService.DeleteQmSpecAsync(qmSpecIds);
                return Ok();
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 通过质量规格id查询质量规格
        /// </summary>
        /// <param name="qmSpecId"></param>
        /// <returns></returns>
        [HttpGet("qmspec/id")]
        public async Task<ActionResult<QmSpecResult>> GetQmSpecByIdAsync([FromQuery] int qmSpecId)
        {
            try
            {
                var qmSpec = await _qmSpecService.GetQmSpecByIdAsync(qmSpecId);
                return Ok(qmSpec);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }

        /// <summary>
        /// 分页查询质量规格
        /// </summary>
        /// <param name="qmSpecPagedinput"></param>
        /// <returns></returns>
        [HttpGet("qmspecs")]
        public async Task<ActionResult<PagedItemsData<QmSpecResult>>> GetQmSpecsAsync([FromQuery] QmSpecPagedInput qmSpecPagedinput)
        {
            try
            {
                var input = await _qmSpecPagedValidator.ValidateAsync(qmSpecPagedinput);
                if (!input.IsValid)
                {
                    return BadRequest(input.Errors.First().ErrorMessage);
                }

                var qmSpecs = await _qmSpecService.GetQmSpecsAsync(qmSpecPagedinput);
                return Ok(qmSpecs);
            }
            catch (CustomException ex)
            {
                _logger.LogError(ex.Message);
                return BadRequest(ex.Message);
            }
        }
    }
}
